package com.fanwh.healthycampus2.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.fanwh.healthycampus2.R;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class RegisterActivity extends AppCompatActivity {

    private EditText studentID;
    private EditText password;
    private Button bt_register;
    private ImageButton back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //透明状态栏          
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        //连接后端云Bmob服务器
        Bmob.initialize(this, "e85fe1093bc0f95a7ef09f6e23de1cae");

        studentID = findViewById(R.id.et_register_studentID);
        password = findViewById(R.id.et_register_password);
        bt_register = findViewById(R.id.bt_register_submit);
        back = findViewById(R.id.ib_navigation_back);

        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取用户输入的账号和密码
                String new_studentID = studentID.getText().toString();
                String new_password = password.getText().toString();
                BmobUser user = new BmobUser();
                user.setUsername(new_studentID);
                user.setPassword(new_password);
                //注册
                user.signUp(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser user, BmobException e){
                        if(e == null){
                            Toast.makeText(RegisterActivity.this,"注册成功"+"学号:"+new_studentID+"密码:"+new_password,Toast.LENGTH_LONG).show();
                            finish();
                        }else{
                            Toast.makeText(RegisterActivity.this,"注册失败"+e.getMessage(),Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}

